
# =====================================
# solve error：UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX
# =====================================
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030') #改变标准输出的默认编码


from urllib import request
from urllib import parse
from http.cookiejar import MozillaCookieJar

url='http://www.baidu.com'
cookiejar = MozillaCookieJar('cookie.txt')
# cookiejar.load() # 加载
handler = request.HTTPCookieProcessor(cookiejar)
opener = request.build_opener(handler)
resp = opener.open(url)

cookiejar.save()
#cookiejar.save(ignore_discard=True, ignore_expires=True) # 用来保存过期的cookies

# load cookies